<?php


define('HTTP_PICK_ADDRESS_ERROR',413);
define('HTTP_REC_ADDRESS_ERROR',414);
define('HTTP_MONEY_NOT_ENOUGH',450);
define('HTTP_SITE_ERROR',456);
define('HTTP_SITE_NOT_ERROR',451);




/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2018/10/12
 * Time: 下午5:37
 */
function getCurrentSite(){
    $site = \App\Models\System\SystemSite::select('id','name','code')->find(getCurrentSiteId());
    if($site){
        return $site->toArray();
    }else{
        apiError('城市未选择',[],HTTP_SITE_ERROR);
    }
}

function getCurrentSiteId(){

    $city = Request()->header('city') ?? 1;

    return intval($city);
}

function setCurrentSiteId($id){
//    $authRole=new \App\Models\Auth\AuthRole();
//    $data= $authRole->getCurrentUserScopeId(true);
//     if($data===true){
    Request()->headers->set('city',$id);
//     }else{
//         if(in_array($id,$data)){
//             Request()->headers->set('city',$id);
//         }else{
//           return  apiError('该用户没有切换该城市的权限','','413');
//         }
//     }
}


/**
 * @param $name
 * @param $key
 * @return mixed
 * 获取配置名称
 */
function getSchemas($name,$key){

    $schemas = \Larfree\Libs\Schemas::getSchemas($name);
    foreach ($schemas as $skey=>$schema){
        if($skey == $key){
            return $schema['name'];
        }
    }
}

/**
 * @return mixed
 * 根据IP定位城市
 */
function getCity(){
    $ip = request()->getClientIp();
    return app('address_parse')->getGeoIpCity($ip);
}

function getCityCode($site){
    switch ($site){
        case 1:
            $cityCode = '010';
            break;
        case 2:
            $cityCode = '021';
            break;
        case 3:
            $cityCode = '0755';
            break;
        case 4:
            $cityCode = '020';
            break;
    }
    return $cityCode;
}

/**
 * @param $address
 * @return mixed
 * 地址纠偏解析高德和百度然后计算相差距离
 */
function getLocation($address){
    $city = getCurrentSite();
    return app('address_parse')->getAddress($address,$city['name'],1);
}

/**
 * @param $filename
 * @param $width
 * @param $height
 * @param int $mode
 * @return string
 * 生成缩略图
 */
function getThumbs($filename, $width, $height, $mode = 0)
{
    if (!$filename)
        return '';
    $type = env('UPLOAD_TYPE','qiniu');
    switch ($type) {
        case 'file':
            return env('APP_URL') . '/' . $filename . "?imageView2/{$mode}/w/{$width}/h/{$height}";
            break;
        default:
            $disk = \Storage::disk('qiniu'); //使用七牛云上传
            if ($mode == '-1') {
                return $disk->downloadUrl($filename)->__toString();//裁剪
            } else {
                return $disk->imagePreviewUrl($filename, "imageView2/{$mode}/w/{$width}/h/{$height}")->__toString();//裁剪
            }
        case 'oss':
            $filename = $filename.'?x-oss-process=image/resize,l_'.$width;
            return $filename.'&x-oss-process=image/crop,w_'.$width.',h_'.$height.',g_center';
            break;
    }
}

function getWeek(){
    $date = date('l');

    switch ($date){
        case 'Monday':
            $name = 1;
            break;
        case 'Tuesday':
            $name = 2;
            break;
        case 'Wednesday':
            $name = 3;
            break;
        case 'Thursday':
            $name = 4;
            break;
        case 'Friday':
            $name = 5;
            break;
        case 'Saturday':
            $name = 6;
            break;
        case 'Sunday':
            $name = 7;
            break;
    }
    return $name;
}


if( !function_exists('LogData')){
    function LogData($title=''){
        //手动记录日志。Laravel 有些地方不知道模型事件
        $action = \Route::current();
        if (!$action)
        {
            $class='未知';
            $method='未知';
        }else{
            $action = $action->getActionName();
            list($class, $method) = explode('@', $action);
        }
        $data['model'] = $class;
        $data['method'] = $method;
        $data['ip']=\Request()->getClientIp();
        $data['user_id']= getLoginUserID()??0;
        $data['key'] = 0;
        $data['type']=1;
        $data['after_content']=null;
        $data['title']=$title;
        $data['before_content']=null;
        $model = 'App\Models\User\\' . ucfirst('userActionLog');
        if (class_exists($model)) {
            $log = new $model();
            if($log->create($data)){
                return true;
            }
        }

    }
}

/**
 * 把返回的数据集转换成Tree
 * @access public
 *  User: xiao
 * @param array $list 要转换的数据集
 * @param string $pid parent标记字段
 * @param string $level level标记字段
 * @return array
 */
if( !function_exists('listToTree')) {
    function listToTree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] =& $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
}

if( !function_exists('treeChild')) {
    function treeChild($id, $result = array())
    {
        global $result;
        $list = \App\Models\Common\CommonUser::query()->where('parent_id', $id)->get();
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                $result[] = $val->id;
                treeChild($val->id, $result);
            }
        }
        return $result;
    }
}